// pages/my/question/index.js
var App = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        question: '',
        length: 0,
        allLength: 1,
        active: null,
        time: false,
        id: 0,
        result: [],
        button: "下一步",
        question: "",
        length: "",
        subId: "",
        subTitle: "",
        subInfo: "",
        title: "",
        api: App.api,
        list: [],
        result: [],
        quertionId: [],
        ide: 0,
        width: 0,
        precent: 0,
        idd: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        var id = options.id;
        var idd = options.idd;
        that.testContent(id);

        that.setData({
            id: id,
            idd: idd
        })
    },
    //保存用户测试
    saveUserTest: function(id) {
        var that = this;
        App.ajax.ajax('testUserSchedule.do?method=saveUserTest', {
            JSESSIONID: wx.getStorageSync('JSESSIONID'),
            i_bb_test_schedule_id: id
        }, function(res) {
            that.setData({
                result: res.data.result
            })
        })
    },
    //请求测试题
    testContent: function(id) {
        var that = this;
        App.ajax.ajax('testUserSchedule.do?method=testInfo', {
            JSESSIONID: wx.getStorageSync('JSESSIONID'),
            id: id,
            subId: ''
        }, function(res) {
            console.log(res.data);
            var id = res.data.subId;
            var list = that.data.list;
            list.push(id);
            that.saveUserTest(res.data.id);
            var length = Math.ceil(res.data.subCount);
            var width = (100 / length).toFixed(3);
            console.log(width)
            that.setData({
                question: res.data.answerMap,
                length: res.data.subCount,
                subId: res.data.subId,
                subTitle: res.data.subTitle,
                subInfo: res.data.subInfo,
                title: res.data.title,
                list: list,
                width: width,
                precent: width
            })
        })
    },
    //选择选项
    select: function(e) {
        var that = this;
        var result = [];
        var ide = e.currentTarget.dataset.id;
        var index = e.currentTarget.dataset.index;
        that.setData({
            active: index,
            ide: ide
        })

    },
    //下一步
    nextStep: function(res) {
        var that = this;
        var subId = that.data.subId;
        var width = that.data.width;
        var long = that.data.allLength;
        var ide = that.data.ide;
        var quertionId = that.data.quertionId;
        quertionId.push(ide);
        //	console.log(width)
        if (long < that.data.length - 1) {
            if (that.data.active == null) {
                wx.showToast({
                    title: '至少选择一个选项',
                    icon: 'none'
                })
            } else {
                that.setData({
                    time: true
                })
                App.ajax.ajax('testUserSchedule.do?method=testInfo', {
                    JSESSIONID: wx.getStorageSync('JSESSIONID'),
                    id: that.data.id,
                    subId: subId
                }, function(res) {
                    console.log(res.data)
                    var id = res.data.subId;
                    var list = that.data.list;
                    list.push(id);
                    if (res.data.jsonType == 'success') {

                        that.setData({
                            question: res.data.answerMap,
                            allLength: long + 1,
                            subId: res.data.subId,
                            subTitle: res.data.subTitle,
                            list: list,
                            quertionId: quertionId,
                            active: null,
                            width: parseInt(width) + parseInt(that.data.precent)
                        })
                        //console.log(that.data.width)
                        setTimeout(function() {
                            that.setData({
                                time: false
                            })
                        }, 900)
                    }
                })
            }
        } else if (that.data.allLength == that.data.length) {
            if (that.data.active == null) {
                wx.showToast({
                    title: '至少选择一个选项',
                    icon: 'none'
                })
            } else {


                console.log(2)
                that.setData({
                    quertionId: quertionId,
                    active: null
                })
                var datas = that.data.quertionId.join(",");
                App.ajax.ajax('testUserSchedule.do?method=saveTestAnswer', {
                    JSESSIONID: wx.getStorageSync('JSESSIONID'),
                    i_bb_user_test_id: that.data.idd,
                    answerList: datas,
                }, function(res) {
                    console.log(res.data)
                    if (res.data.jsonType == 'success') {
                        console.log(res.data)
                        wx.showToast({
                            title: '提交成功',
                            icon: "success",
                            success: function(res) {
                                setTimeout(function() {
                                    wx.switchTab({
                                        url: '../../test/index/index',
                                    })
                                }, 1500)
                            }
                        })
                    }
                })
            }
        } else if (long == that.data.length - 1) {
            if (that.data.active == null) {
                wx.showToast({
                    title: '至少选择一个选项',
                    icon: 'none'
                })
            } else {
                var that = this;
                var subId = that.data.subId;
                var width = that.data.width;
                var long = that.data.allLength;
                that.setData({
                    time: true
                })
                App.ajax.ajax('testUserSchedule.do?method=testInfo', {
                    JSESSIONID: wx.getStorageSync('JSESSIONID'),
                    id: that.data.id,
                    subId: subId
                }, function(res) {
                    var id = res.data.subId;
                    var list = that.data.list;
                    list.push(id);
                    that.setData({
                        question: res.data.answerMap,
                        allLength: long + 1,
                        subId: res.data.subId,
                        subTitle: res.data.subTitle,
                        button: "提交结果",
                        list: list,
                        quertionId: quertionId,
                        active: null,
                        width: parseInt(width) + parseInt(that.data.precent)
                    })
                    setTimeout(function() {
                        that.setData({
                            time: false,
                        })
                    }, 900)
                })
            }
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})